//
//  SimpleWebImageTests.m
//  SimpleWebImageTests
//
//  Created by Jiang on 2019/8/26.
//  Copyright © 2019 SilverFruity. All rights reserved.
//

#import <XCTest/XCTest.h>
#import <SimpleWebImage/SimpleWebImage.h>

@interface SimpleWebImageTests : XCTestCase

@end

@implementation SimpleWebImageTests

- (void)setUp {
    // Put setup code here. This method is called before the invocation of each test method in the class.
}

- (void)tearDown {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
}

- (void)testOperationPool{
    SimpleImageOperationPool *pool = [SimpleImageOperationPool new];
    [pool addOperation:[SimpleImageOperation new] forKey:@"test"];
    [pool addOperation:[SimpleImageOperation new] forKey:@"test"];
    [pool addOperation:[SimpleImageOperation new] forKey:@"test1"];
    XCTAssert([pool operationsForKey:@"test"].count == 2);
    XCTAssert([pool operationsForKey:@"test1"].count == 1);
}
- (void)testOperationCancel{
    SimpleWebImageManager *manager = [SimpleWebImageManager shareInstance];
    NSURL *url = [NSURL URLWithString:@"http://via.placeholder.com/50x50.jpg"];
    __block hasExe = NO;
    SimpleImageOperation *op = [SimpleWebImageManager.shareInstance loadImageWithUrl:url progress:nil complete:^(UIImage * _Nonnull image, NSURL * _Nonnull url, NSError * _Nullable error) {
        hasExe = YES;
    }];
    op.isCancel = YES;
    sleep(3);
    XCTAssert(hasExe == NO);
    XCTAssert([manager.diskCache imageForKey:[manager cacheKeyForUrl:url]] != nil);
    XCTAssert([manager.memoryCache imageForKey:[manager cacheKeyForUrl:url]] != nil);
    
    [manager.diskCache removeImageForKey:[manager cacheKeyForUrl:url]];
    XCTAssert([manager.diskCache imageForKey:[manager cacheKeyForUrl:url]] == nil);
    
    [manager.memoryCache removeImageForKey:[manager cacheKeyForUrl:url]];
    XCTAssert([manager.memoryCache imageForKey:[manager cacheKeyForUrl:url]] == nil);
}
- (void)testOperationRun{
    XCTestExpectation *exp = [self expectationWithDescription:@"testOperationRun"];
    SimpleWebImageManager *manager = [SimpleWebImageManager shareInstance];
    NSURL *url = [NSURL URLWithString:@"http://via.placeholder.com/50x50.jpg"];
    __block double _progress = 0;
    [SimpleWebImageManager.shareInstance loadImageWithUrl:url progress:^(double progress, NSURL * _Nonnull url) {
        _progress = progress;
    } complete:^(UIImage * _Nonnull image, NSURL * _Nonnull url, NSError * _Nullable error) {
        [exp fulfill];
    }];
    [self waitForExpectationsWithTimeout:5 handler:^(NSError *error) {
        //等待10秒，若该测试未结束（未收到 fulfill方法）则测试结果为失败
        //Do something when time out
    }];
    XCTAssert(_progress != 0);
    
    NSLog(@"%@",[manager.diskCache imageForKey:[manager cacheKeyForUrl:url]]);
    
    XCTAssert([manager.diskCache imageForKey:[manager cacheKeyForUrl:url]] != nil);
    XCTAssert([manager.memoryCache imageForKey:[manager cacheKeyForUrl:url]] != nil);
    
    [manager.diskCache removeImageForKey:[manager cacheKeyForUrl:url]];
    XCTAssert([manager.diskCache imageForKey:[manager cacheKeyForUrl:url]] == nil);
    
    [manager.memoryCache removeImageForKey:[manager cacheKeyForUrl:url]];
    XCTAssert([manager.memoryCache imageForKey:[manager cacheKeyForUrl:url]] == nil);
    
    
}


- (void)testPerformanceExample {
    // This is an example of a performance test case.
    [self measureBlock:^{
        // Put the code you want to measure the time of here.
    }];
}

@end
